package com.platform.controller;

import com.platform.entity.DynamicCateMappingEntity;
import com.platform.entity.DynamicPictureEntity;
import com.platform.entity.UserDynamicEntity;
import com.platform.service.DynamicCateMappingService;
import com.platform.service.DynamicPictureService;
import com.platform.service.UserDynamicService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Controller
 *
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2018-08-21 17:19:56
 */
@RestController
@RequestMapping("userdynamic")
public class UserDynamicController {
    @Autowired
    private UserDynamicService userDynamicService;
    @Autowired
    private DynamicPictureService commentPictureService;

    @Autowired
    private DynamicCateMappingService dynamicCateMappingService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("dynamic:list")
    public R list(@RequestParam Map<String, Object> params) {

        //查询列表数据
        Query query = new Query(params);

        List<UserDynamicEntity> cmsNewsList = userDynamicService.queryList(query);
        int total = userDynamicService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(cmsNewsList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("dynamic:info")
    public R info(@PathVariable("id") Integer id) {
        UserDynamicEntity userDynamicEntity = userDynamicService.queryObject(id);


        Map map = new HashMap();
        map.put("newsid", id);
        //获取动态挂载的分类
        List<DynamicCateMappingEntity> catetgoryNews = dynamicCateMappingService.queryList(map);
        List<Integer> categoryIDs = new ArrayList();
        for (int i = 0; i < catetgoryNews.size(); i++) {
            DynamicCateMappingEntity catetgoryNew = catetgoryNews.get(i);
            categoryIDs.add(catetgoryNew.getCategoryid());
        }
        userDynamicEntity.setCategoryIDlist(categoryIDs);
        Map<String, Object> query = new HashMap<String, Object>();
        query.put("newid", id);
        List<DynamicPictureEntity> picList = commentPictureService.queryList(query);

        if (picList != null && picList.size() > 0) {
            String dynamicPicture = "";
            String video = "";
            for (DynamicPictureEntity sportsCommentPictureEntity : picList) {
               if(sportsCommentPictureEntity.getType().equals(2)){
                    video = sportsCommentPictureEntity.getPicUrl();
               }else{
                   dynamicPicture = dynamicPicture +"<img src=\"" + sportsCommentPictureEntity.getPicUrl() + "\" alt=\"\" />";
               }
            }
            userDynamicEntity.setDynamicPicture(dynamicPicture);
            userDynamicEntity.setVideo(video);

        }
        return R.ok().put("cmsNews", userDynamicEntity);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("dynamic:save")
    public R save(@RequestBody UserDynamicEntity cmsNews) {

        userDynamicService.save(cmsNews);
        //后保存
        List<Integer> categoryIDs = cmsNews.getCategoryIDlist();
        if (categoryIDs != null && categoryIDs.size() > 0) {
            for (int i = 0; i < categoryIDs.size(); i++) {
                DynamicCateMappingEntity catetgoryNew = new DynamicCateMappingEntity();
                catetgoryNew.setNewsid(cmsNews.getId());
                catetgoryNew.setCategoryid(categoryIDs.get(i));
                dynamicCateMappingService.save(catetgoryNew);
            }
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("dynamic:update")
    public R update(@RequestBody UserDynamicEntity cmsNews) {
        int i = userDynamicService.update(cmsNews);
        if (i <= 0) {
            return R.error("奖励失败请重新修改保存");
        }

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("dynamic:delete")
    public R delete(@RequestBody Integer[] ids) {
        userDynamicService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {
        List<UserDynamicEntity> list = userDynamicService.queryList(params);
        return R.ok().put("list", list);
    }

    @PostMapping(value = "/check")
    @RequiresPermissions("dynamic:check")
    public R check(@RequestBody Integer[] ids) {

        int check = userDynamicService.check(ids);
        if (check > 0) {
            return R.ok();
        } else {
            return R.error();
        }

    }
}
